from rest_framework.response import Response
from rest_framework.views import exception_handler
from django.db import DatabaseError
import logging

log = logging.getLogger("django")

# 在产生结果的地方，记录日志
# log.info("")
# log.error("")

def custom_exception_handler(exc, context):
    response = exception_handler(exc, context)

    if response is None and isinstance(exc, DatabaseError):
        log.error("当前服务器数据库出错：%s" % exc)
        return Response({'message': '服务器内部错误'}, status=500)

    return response